package com.cloud.thread;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/7/8 15:38
 * @Desc
 * @Remarks
 */

//强，弱，软，虚四种引用


public class ThreadLocalDemo {
    public static final ThreadLocal<String> threadLocal = new ThreadLocal<>();



    public static void main(String[] args) {

        ServiceDemo serviceDemo = new ServiceDemo();
        new Thread(() -> {
            threadLocal.set("我是线程1");
            serviceDemo.method();
        }).start();

        new Thread(() -> {
            threadLocal.set("我是线程2");
            serviceDemo.method();
        }).start();

    }
}

class ServiceDemo {
    public void method() {
//        获取threadLocal中保存的数据
        System.out.println(ThreadLocalDemo.threadLocal.get());
    }
}